package Demo5;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 86136
 * Date: 2024-05-23
 * Time: 16:21
 */
public class Test {
    //方法传参：形参为父类型引用，可以接收任意子类的对象
    public static void eat(Animal animal) {
        animal.eat();
    }

    //返回值：返回任意子类对象
    public static Animal fuc() {
        Dog dog = new Dog("小黑狗",2);
        return dog;
    }
    public static void main(String[] args) {
        /*
        向上转型赋值的三种形式：直接赋值、方法赋值、返回值
         */
        //直接赋值：把子类对象赋给父类对象
        /*Dog dog = new Dog("元宝",2);
        Animal animal = dog;*/
        System.out.println("直接赋值：");
        Animal animal = new Dog("元宝狗",1);
        animal.eat();
        System.out.println("------------");
        System.out.println("方法赋值：");
        Dog dog = new Dog("大黄狗",3);
        Cat cat = new Cat("大白猫",3);
        eat(dog);
        eat(cat);
        System.out.println("------------");
        System.out.println("返回值：");
        Animal animal1 = fuc();
        animal1.eat();


    }
}
